<?php

namespace server\impl;

use common\core\server\BaseServer;
use server\dao\AuthAssignment;
use server\dao\AuthItem;
use server\dao\AuthItemChild;
use server\dao\AuthRule;
use server\dao\Role;
use server\dao\User;
use server\RoleInterface;

class RoleImpl extends BaseServer implements RoleInterface
{
    public $user;
    public $auth_item;
    public $auth_assignment;
    public $auth_rule;
    public $auth_item_child;

    public function __construct(array $config = [])
    {
        $this->user = new User();
        //$this->auth_item = new AuthItem();
        $this->auth_item = new Role();
        $this->auth_assignment = new AuthAssignment();
        $this->auth_rule = new AuthRule();
        $this->auth_item_child = new AuthItemChild();
        parent::__construct($config);
    }

    public function getRoleList()
    {
        return $this->auth_item->getRoleList();
    }

    public function getPermissionList()
    {
        //$this->auth_item->assignPermission("用户管理",10);
        //$this->auth_item->assign("测试角色名称",10);
        //$this->auth_item->revokeAll(10);
        return $this->auth_item->getPermissionList();
    }

    public function assign(array $data)
    {
        if (!empty($data['roles'])) {
            $this->auth_item->revokeAll($data['user_id']);
            foreach ($data['roles'] as $item) {
                $this->auth_item->assign($item, $data['user_id']);
                /*if ($item['type'] == 1) {
                    $this->auth_item->assign($item['name'], $data['user_id']);
                } elseif ($item['type'] == 2) {
                    $this->auth_item->assignPermission($item['name'], $data['user_id']);
                }*/
            }
        }
    }

    public function getRolesByUser($usr_id)
    {
        return $this->auth_item->getRolesByUser($usr_id);
    }

    public function getChildRolePermission($name)
    {
        return $this->auth_item->getChildRoles($name);
    }
}
